#include <iostream>
#include <vector>

#include "../include/tensor.h"
using namespace std;

void test_equal()
{
    Value a(2.0, "a"), b(3.0, "b");
    Value c = a + b;
    c.label = "c";

    a.grad = 0.0, b.grad = 0.0;
    c.backward(true);
    cout << a << endl
         << b << endl
         << c;
    cout << endl;

    a.grad = 0.0, b.grad = 0.0;
    Value d = c + a;
    d.label = "d";
    cout << "Value d = c + a; d.prev:" << endl;
    for (auto tmp : d._prev)
        cout << tmp->label << endl; // Value d = c + a; d.prev: c a
    cout << endl
         << "c = d; c.prev:" << endl;
    c = d;
    for (auto tmp : c._prev)
        cout << tmp->label << endl; // c = d; c.prev: d a
    cout << endl
         << "c = d; d.prev:" << endl;
    for (auto tmp : d._prev)
        cout << tmp->label << endl; // c = d; d.prev: d a
    // loop, if backward.topo not use visited-set, we will get a loop: c - d - d - d - d...
    // The original reason is d has changed c, which we don't want.
    // What we want is new_c = d, d.prev has old_c, and old_c has no assocition with new_c.
    // So we can't reuse c, while We can use point to do this as shown in tensor.h.docs[1].
}
int main()
{
    test_equal();
}